import { Injectable, Logger } from "@nestjs/common";
import { ConfigService } from '@nestjs/config';
import axios from 'axios';

@Injectable()
export class ZrwinfoService {
  private readonly messageUrl: string;
  private readonly sign: string;
  private readonly templateId: string;

  private readonly realNameUrl2: string;
  private readonly realNameUrl3: string;
  private readonly accessKey: string;
  private readonly secret: string;

  constructor(private readonly config: ConfigService) {
    this.messageUrl = this.config.get('zrwinfo.messageUrl');
    this.sign = this.config.get('zrwinfo.sign');
    this.templateId = this.config.get('zrwinfo.templateId');

    this.realNameUrl3 = this.config.get('zrwinfo.realNameUrl3');
    this.realNameUrl2 = this.config.get('zrwinfo.realNameUrl2');
    this.accessKey = this.config.get('zrwinfo.accessKey');
    this.secret = this.config.get('zrwinfo.secret');
  }

  public async sendMessage(phone: string, code: string): Promise<any> {
    return await axios
      .post(
        this.messageUrl,
        {
          accesskey: this.accessKey,
          secret: this.secret,
          sign: this.sign,
          templateId: this.templateId,
          mobile: phone,
          content: code,
        },
        {
          headers: {
            'Content-Type': 'application/json',
          },
        },
      )
      .then((res) => res.data);
  }

  public async sendRefund(phone: string) {
    return await this.sendMessage(phone, '0');
  }

  public async simpleThreeElements(
    username: string,
    idCard: string,
    phone: string,
  ) {
    return await axios
      .post(
        this.realNameUrl3,
        {
          accesskey: this.accessKey,
          secret: this.secret,
          realname: username,
          idcard: idCard,
          mobile: phone,
        },
        {
          headers: {
            'Content-Type': 'application/json',
          },
        },
      )
      .then((res) => res.data);
  }

  public async simpleTwoElements(username: string, idCard: string) {
    return await axios
      .post(
        this.realNameUrl2,
        {
          accesskey: this.accessKey,
          secret: this.secret,
          realname: username,
          idcard: idCard,
        },
        {
          headers: {
            'Content-Type': 'application/json',
          },
        },
      )
      .then((res) => res.data);
  }
}
